package com.shiliao.app.weight;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.shiliao.app.R;
import com.yimei.framework.base.ui.BaseActivity;

import butterknife.BindView;


public class WebViewUtils extends BaseActivity {

    @BindView(R.id.webview)
    ProgressWebView webview;

    @BindView(R.id.actionbar)
    public ActionBarView barView;

    @BindView(R.id.scroll_top)
    ImageView mScrollTop;

    private String mTitle;


    @Override
    public int getLayoutId() {
        return R.layout.webview;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView() {
        super.initView();
        barView.getBackView().setVisibility(View.VISIBLE);
        barView.setBackClick(this);
        Intent it = getIntent();
        initWebview();
        Bundle bundle = it.getExtras();
        String url = bundle.getString("url");
        mTitle = bundle.getString("title");
        barView.setTitles(mTitle);
        webview.setWebViewClient(new WebViewClient());
        if (url != null) {
            if (url != null && !url.contains("http://")
                    && !url.contains("https://")) {
                url = "http://" + url;
            }
            webview.loadUrl(url);
            //  webview.loadData(url,"application/raw","utf-8");

        } else if (bundle.containsKey("file")) {

            String fileName = bundle.getString("file");

            if (fileName != null) {
                String strUrl = "file:///android_asset/" + fileName;

                webview.loadUrl(strUrl);
            }

        } else {

            String html = bundle.getString("html");
            if (html != null) {
                webview.loadDataWithBaseURL(null, html, "text/html", "utf-8",
                        null);
            }

        }
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebview() {
        webview.setInitialScale(28);
        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);// 设置允许访问文件数据
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
//        webview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                if (scrollY > 0) {
//                    mScrollTop.setVisibility(View.VISIBLE);
//                } else {
//                    mScrollTop.setVisibility(View.GONE);
//                }
//            }
//        });

    }

}
